package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.bruce.common.utils.FileUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.user.PasswdModifyParams;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.ui.user.iView.PasswdModifyIView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-21 09:33
 */
public class PasswdModifyPresenter {

    private PasswdModifyIView iView;

    public PasswdModifyPresenter(PasswdModifyIView iView) {
        this.iView = iView;
    }
    
    public void modifyPasswd(final String oldPwd, final String newPwd){
        PasswdModifyParams params = new PasswdModifyParams();
        params.userId = UserUtil.getUserId();
        params.oldPwd = FileUtil.MD5(oldPwd);
        params.newPwd = FileUtil.MD5(newPwd);
        API.http().post("modifyPasswd", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse res = JSON.parseObject(result, CommonResponse.class);
                    if (res != null && Status.SUCCESS.equals(res.getStatus())) {
                        iView.onModifySuccess();
                    } else {
                        iView.onModifyFailure("原密码错误");
                    }
                } else {
                    iView.onModifyFailure("密码修改失败，请稍后重试");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onModifyFailure("密码修改失败，请稍后重试");
            }
        });
    }

}
